<!--
@Description:业务部门与业务类型关系配置表
@Author:zqy
@Date:2022-05-11 15:58:52
@Last Modified by:zqy
@Last Modified time:2022-05-11 15:58:52
-->
<template>
  <div class="contentRel">
    <ContentWrap style="padding-top:5px">
      <search-form ref="refSearch" :search-config="searchConfig" :copyable="false" label-width="120" @search="handleSearch" />
      <div class="button-container" style="justify-content:flex-start;padding-top:10px">
        <ContentRelAdd text="新增" title="新增" @on-success="onSuccessHandle" />
        <ContentRelAdd text="编辑" title="编辑" :row="tableSelected[0]" @on-success="onSuccessHandle" />
        <Button type="danger" @click="handleDelete">删除</Button>
      </div>
      <table-box
        ref="refTable"
        table-size="small"
        :table-config="tableConfig"
        :other-filters="otherFilters"
        :off-set="-50"
        :show-index="false"
        :show-selection="true"
        :show-custom-setting="false"
        :table-sortable="false"
        :selected-arry.sync="tableSelected"
        @selection-change="handleselectionChange"
        @row-click="handleRowClick"
      />
    </ContentWrap>
  </div>
</template>
<script>
import ContentWrap from '@/components/ContentWrap'
import ContentRelAdd from './components/contentRelAdd'
import { deleteBatch } from '@/api/BusinessDepartRel'
import { parseTime } from '@/utils'
export default {
  name: 'ContentRel',
  components: {
    ContentWrap,
    ContentRelAdd
  },
  data() {
    return {
      searchConfig: [
        {
          label: '一级业务类型', prop: 'businessType', type: 'select', value: '',
          config: {
            remote: true,
            remoteUrl: `/data-dict/api/v2/dictionary/FIRST_LEVEL_BUSINESS_TYPE`,
            defaultProps: { label: 'dictName', value: 'dictCode' },
            paramsRaw: {
              appCode: 'call-center',
              domainCode: 'marketing'
            }
          }
        },
        {
          label: '二级业务类型', prop: 'businessTypeTwo', type: 'select', value: '',
          config: {
            remote: true,
            remoteUrl: `/data-dict/api/v2/dictionary/SECOND_LEVEL_BUSINESS_TYPE`,
            defaultProps: { label: 'dictName', value: 'dictCode' },
            paramsRaw: {
              appCode: 'call-center',
              domainCode: 'marketing'
            }
          }
        },
        {
          label: '责任部门', prop: 'dept', type: 'select', value: '',
          multiple: true,
          config: {
            remote: true,
            remoteUrl: `/data-dict/api/v2/dictionary/RESPONSIBLE_DEPARTMENT`,
            defaultProps: { label: 'dictName', value: 'dictCode' },
            paramsRaw: {
              appCode: 'call-center',
              domainCode: 'marketing'
            }
          }
        }
      ],
      otherFilters: [], // 表格额外查询条件
      otherRaws: {},
      tableSelected: [], // 选择列
      tableConfig: {
        interfacePath: `/call-center/api/v2/businessDepartmentRelation/page`,
        tabIndex: 0,
        hideFuncBar: true,
        sort: 'createTime',
        order: 'desc',
        setting: [
          {
            prop: 'businessTypeName',
            label: '一级业务类型',
            minWidth: 150
          },
          {
            prop: 'businessTypeTwoName',
            label: '二级业务类型',
            minWidth: 150
          },
          {
            prop: 'deptName',
            label: '默认责任部门',
            minWidth: 150
          },
          {
            prop: 'createBy',
            label: '创建人',
            minWidth: 150
          },
          {
            prop: 'createTime',
            label: '创建时间',
            minWidth: 150,
            formatData: ({ createTime }) => {
              return parseTime(createTime, '{y}-{m}-{d} {h}:{i}:{s}')
            }
          },

          {
            prop: 'lastUpdateBy',
            label: '最近修改人',
            minWidth: 150
          },
          {
            prop: 'lastUpdateTime',
            label: '最近修改时间',
            minWidth: 150,
            formatData: ({ lastUpdateTime }) => {
              return parseTime(lastUpdateTime, '{y}-{m}-{d} {h}:{i}:{s}')
            }
          }
        ]
      },
      activeRowData: '',
      showAdd: false
    }
  },
  created() {

  },
  methods: {
    handleRowClick({ row }) {
      this.$refs.refTable.clearSelection()
      this.$nextTick(() => {
        this.$refs.refTable.toggleRowSelection(row, true)
        this.activeRowData = row || ''
      })
    },
    handleselectionChange(val) {
      this.tableSelected = val
    },
    // 搜索项联动
    handleSearchEditChange({ prop, value }) {
      if (prop === 'businessType') {
        let config = {
          remote: false,
          remoteUrl: '',
          selectData: []
        }
        if (value) {
          config = {
            remote: true,
            remoteUrl: `/call-center/api/v1/businessDeptRelation/getByParentRelationCode/${value}`,
            defaultProps: { label: 'relationName', value: 'relationCode' }
          }
        }
        this.$refs.refSearch.initSelectData('businessTypeTwo', config)
        this.$refs.refSearch.updateValue('businessTypeTwo', '')
      }
    },
    // 搜索
    handleSearch(params) {
      this.otherFilters = params.filtersRaw || []
      this.otherRaws = params.paramsRaw || {}
      this.refreshTable(true)
    },
    // 刷新主表
    refreshTable(isInit) {
      this.$nextTick(() => {
        this.$refs.refTable && this.$refs.refTable.initTableData(isInit ? '' : 'onPage')
        this.activeRowData = ''
        this.tableSelected = []
      })
    },
    // 添加成功刷新数据
    onSuccessHandle(isNew) {
      this.$nextTick(() => {
        this.refreshTable(!!isNew)
      })
    },
    // 删除
    handleDelete(id) {
      if (Array.isArray(this.tableSelected) && this.tableSelected.length === 0) {
        this.$message({
          message: '请选择需删除的数据',
          type: 'warning'
        })
        return
      }
      this.$confirm('此操作将永久删除当前所选数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = this.tableSelected.map(v => v.id)
        deleteBatch(ids).then(res => {
          if (res.success === 1) {
            this.$message.success('删除成功')
            this.refreshTable()
          }
        })
      }).catch(() => {})
    }
  }
}
</script>
<style lang='scss' scoped>

</style>
